package com.xbongbong.pro.rabbitmq.consumer;

import com.alibaba.fastjson.JSON;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.message.enums.PushTypeEnum;
import com.xbongbong.pro.message.pojo.dto.MessageRabbitMqDTO;
import com.xbongbong.pro.message.pojo.vo.MessageRabbitMqVO;
import com.xbongbong.pro.rabbitmq.binders.OfficeConsumerBinder;
import com.xbongbong.pro.rabbitmq.constant.ApprovalBinderConstant;
import com.xbongbong.pro.rabbitmq.constant.CooperativeOfficeBinderConstant;
import com.xbongbong.pro.rabbitmq.constant.DownloadBinderConstant;
import com.xbongbong.pro.rabbitmq.constant.WorkOrderBinderConstant;
import com.xbongbong.pro.rabbitmq.feign.ApprovalFeignClient;
import com.xbongbong.pro.rabbitmq.feign.CooperativeOfficeFeignClient;
import com.xbongbong.pro.rabbitmq.feign.DownloadFeignClient;
import com.xbongbong.pro.rabbitmq.feign.WorkOrderFeignClient;
import feign.RetryableException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.cloud.stream.annotation.StreamListener;
import org.springframework.messaging.handler.annotation.Header;

import javax.annotation.Resource;

/**
 * 协同办公，工单，下载 审批 消息消费者
 * @author 魏荣杰
 * @date 2019/2/25 14:30
 * @since v1.0
 * @version v1.0
 */
@EnableBinding(OfficeConsumerBinder.class)
public class OfficeListener {
    private static Logger logger = LoggerFactory.getLogger(OfficeListener.class);

    @Resource
    private CooperativeOfficeFeignClient cooperativeOfficeFeignClient;
    @Resource
    private WorkOrderFeignClient workOrderFeignClient;
    @Resource
    private DownloadFeignClient downloadFeignClient;
    @Resource
    private ApprovalFeignClient approvalFeignClient;

    @StreamListener(value = CooperativeOfficeBinderConstant.WORKFLOW_INPUT)
    public void workflowReceive(MessageRabbitMqDTO message, @Header("type") PushTypeEnum type) {
        XbbResponse<MessageRabbitMqVO> response = null;
        try {
            switch (type) {
                case APPROVAL_ADD_PUSH:
                    response = cooperativeOfficeFeignClient.workflowAdd(message);
                    break;
                case APPROVAL_CHANGE_PUSH:
                    response = cooperativeOfficeFeignClient.workflowChange(message);
                    break;
                case APPROVAL_NODE_CHANGE_PUSH:
                    response = cooperativeOfficeFeignClient.workflowNodeChange(message);
                    break;
                case APPROVAL_AGREE_PUSH:
                    response = cooperativeOfficeFeignClient.workflowPass(message);
                    break;
                case APPROVAL_REFUSE_PUSH:
                    response = cooperativeOfficeFeignClient.workflowRefuse(message);
                    break;
                case APPROVAL_REEDIT_PUSH:
                    response = cooperativeOfficeFeignClient.workflowReEdit(message);
                    break;
                case APPROVAL_CARBON_COPY_PUSH:
                    response = cooperativeOfficeFeignClient.workflowToCc(message);
                    break;
                case APPROVAL_COMMENT_AT:
                    response = cooperativeOfficeFeignClient.workflowToAt(message);
                    break;
                case APPROVAL_COMMENT_EDIT:
                    response = cooperativeOfficeFeignClient.workflowCommentEdit(message);
                    break;
                case APPROVAL_COMMENT_DEL:
                    response = cooperativeOfficeFeignClient.workflowCommentDel(message);
                    break;
                default:
                    break;
            }
            logger.info("Received: "  + type.toString() + "\n message=" + JSON.toJSONString(message));
        } catch (RetryableException e) {
            logger.error("OfficeListener.workflowReceive RetryableException case by Read timed out, type=" + type);
        } catch (Exception e) {
            logger.error("OfficeListener.workflowReceive Exception, type=" + type, e);
        }
    }

    @StreamListener(value = WorkOrderBinderConstant.WORK_ORDER_INPUT)
    public void workOrderReceive(MessageRabbitMqDTO message, @Header("type") PushTypeEnum type) {
        XbbResponse<MessageRabbitMqVO> response = null;
        try {
            switch (type) {
                case WORKORDER_PUSH:
                    response = workOrderFeignClient.workOrderPush(message);
                    break;
                case WORKORDER_NEW_EXECUTED:
                    response = workOrderFeignClient.workOrderNewExecuted(message);
                    break;
                case WORKORDER_NEW_EXECUTOR:
                    response = workOrderFeignClient.workOrderNewExecutor(message);
                    break;
                case WORKORDER_CONFIRM:
                    response = workOrderFeignClient.confirm(message);
                    break;
                case WORKORDER_TEAM:
                    response = workOrderFeignClient.team(message);
                    break;
                case WORKORDER_STAGE_COMPLETE:
                    response = workOrderFeignClient.stageComplete(message);
                    break;
                case WORKORDER_COMPLETE:
                    response = workOrderFeignClient.complete(message);
                    break;
                case WORKORDER_CLOSE:
                    response = workOrderFeignClient.close(message);
                    break;
                case WORKORDER_RESTART:
                    response = workOrderFeignClient.restart(message);
                    break;
                case WORKORDER_TRANSFER:
                    response = workOrderFeignClient.transfer(message);
                    break;
                case WORKORDER_ROLLBACK:
                    response = workOrderFeignClient.rollback(message);
                    break;
                case WORKORDER_DEL_PUSH:
                    response = workOrderFeignClient.del(message);
                    break;
                case WORKORDER_DYNAMIC_PUSH:
                    response = workOrderFeignClient.dynamic(message);
                    break;
                case WORKORDER_DYNAMIC_UPDATE_PUSH:
                    response = workOrderFeignClient.dynamicEdit(message);
                    break;
                case WORKORDER_DYNAMIC_DEL_PUSH:
                    response = workOrderFeignClient.dynamicDel(message);
                    break;
                case WORKORDER_COMMENT_LIKE_PUSH:
                    response = workOrderFeignClient.like(message);
                    break;
                case WORKORDER_COMMENT_PUSH:
                    response = workOrderFeignClient.comment(message);
                    break;
                case WORKORDER_COMMENT_AT:
                    response = workOrderFeignClient.at(message);
                    break;
                case WORKORDER_COMMENT_DEL_PUSH:
                    response = workOrderFeignClient.commentDel(message);
                    break;
                default:
                    break;
            }
            logger.info("Received: "  + type.toString() + "\n message=" + JSON.toJSONString(message));
        } catch (RetryableException e) {
            logger.error("OfficeListener.workOrderReceive RetryableException case by Read timed out, type=" + type);
        } catch (Exception e) {
            logger.error("OfficeListener.workOrderReceive Exception, type=" + type, e);
        }
    }

    @StreamListener(value = DownloadBinderConstant.DOWNLOAD_INPUT)
    public void downloadReceive(MessageRabbitMqDTO message, @Header("type") PushTypeEnum type) {
        XbbResponse<MessageRabbitMqVO> response = null;
        try {
            switch (type) {
                case EXPORT_PUSH:
                    response = downloadFeignClient.success(message);
                    break;
                case EXPORT_PUSH_FAIL:
                    response = downloadFeignClient.fail(message);
                default:
                    break;
            }
            logger.info("Received: "  + type.toString() + "\n message=" + JSON.toJSONString(message));
        } catch (RetryableException e) {
            logger.error("OfficeListener.downloadReceive RetryableException case by Read timed out, type=" + type);
        } catch (Exception e) {
            logger.error("OfficeListener.downloadReceive Exception, type=" + type, e);
        }
    }

    @StreamListener(value = ApprovalBinderConstant.APPROVAL_INPUT)
    public void approvalReceive(MessageRabbitMqDTO message, @Header("type") PushTypeEnum type) {
        XbbResponse<MessageRabbitMqVO> response = null;
        try {
            switch (type) {
                case APPROVAL_PUSH_ADD_COMMENT:
                    response = approvalFeignClient.addComment(message);
                default:
                    break;
            }
            logger.info("Received: "  + type.toString() + "\n message=" + JSON.toJSONString(message));
        } catch (RetryableException e) {
            logger.error("OfficeListener.downloadReceive RetryableException case by Read timed out, type=" + type);
        } catch (Exception e) {
            logger.error("OfficeListener.downloadReceive Exception, type=" + type, e);
        }
    }
}
